//
//  oldPayView.m
//  laonianbang
//
//  Created by 勇拓 李 on 18/3/14.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldPayView.h"
#import "AppDelegate.h"
#import "oldHub.h"
#import "oldNetWork.h"
#import <AlipaySDK/AlipaySDK.h>
#import "oldMessageManager.h"
#import "oldBalance.h"

#define PAY_TYPE @"1"
#define RECHARGE_TYPE @"2"

@interface oldPayView()<oldMessageDelegate>
@property (weak, nonatomic) IBOutlet UIButton *btn_weixin;
@property (weak, nonatomic) IBOutlet UIButton *btn_alipay;
@property (weak, nonatomic) IBOutlet UIButton *btn_balance;
@property (weak, nonatomic) IBOutlet UIButton *btn_confirm;
@property (weak, nonatomic) IBOutlet UIView *view_balance;
@property (weak, nonatomic) IBOutlet UIView *view_alipay;
@property (weak, nonatomic) IBOutlet UIView *view_wechat;
@property (nonatomic, strong)oldMessageManager *managerSuccess;
@property (nonatomic, strong)oldMessageManager *managerFailed;
@property (nonatomic, assign)PayType type;
@end

@implementation oldPayView

//NSString *GetPayType(PayType status) {
//    switch (status) {
//        case ALI_PAY:
//            return @"AlipayMobileApp";
//            break;
//        case WECHAT_PAY:
//            return @"WeixinMobileApp";
//            break;
//        default:
//            return @"";
//    }
//}

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    self.layer.cornerRadius = 6.f;
    
    _btn_confirm.layer.cornerRadius = 6.f;
    
    _type = WECHAT_PAY;
    
    [self initMessage];
    
    [_btn_confirm addTarget:self action:@selector(btn_confirm_tapped:) forControlEvents:UIControlEventTouchDown];
//    [_btn_weixin addTarget:self action:@selector(btn_weixin:) forControlEvents:UIControlEventTouchDown];
//    [_btn_alipay addTarget:self action:@selector(btn_alipay_tapped:) forControlEvents:UIControlEventTouchDown];
//    [_btn_balance addTarget:self action:@selector(btn_balance_tapped:) forControlEvents:UIControlEventTouchDown];
    UITapGestureRecognizer *tapWechat = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btn_weixin:)];
    [_view_wechat addGestureRecognizer:tapWechat];
    
    UITapGestureRecognizer *tapAliPay = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btn_alipay_tapped:)];
    [_view_alipay addGestureRecognizer:tapAliPay];
    
    UITapGestureRecognizer *tapBalance = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btn_balance_tapped:)];
    [_view_balance addGestureRecognizer:tapBalance];
    
    UIView *blackView = [UIView new];
    
    blackView.frame = [UIScreen mainScreen].bounds;
    
    [[AppDelegate getMainWindow] addSubview:blackView];
    
    blackView.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0  blue:0/255.0  alpha:.7];
    
    self.frame = CGRectMake(0, 0, 300, 400);
    
    [blackView addSubview:self];
    
    self.center = blackView.center;
    
    //加入点击手势remove分享界面
    UITapGestureRecognizer *tapView=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapChange:)];
    [blackView addGestureRecognizer:tapView];
    
    UITapGestureRecognizer *tapPayView=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPay:)];
    [self addGestureRecognizer:tapPayView];
    
    [self fadeIn];
}

- (void)setRecharge:(BOOL)recharge
{
    _recharge = recharge;
    if (_recharge) {
        _view_balance.hidden = YES;
    }
}

#pragma mark - message
- (void)initMessage
{
    _managerSuccess = [[oldMessageManager alloc] initWithType:paySuccess];
    _managerSuccess.delegate = self;
    [_managerSuccess registerForNotifications];
    
    _managerFailed = [[oldMessageManager alloc] initWithType:payFail];
    _managerFailed.delegate = self;
    [_managerFailed registerForNotifications];
}

- (void)receiveMessage:(id)message type:(oldMessageType)type
{
    if (type == paySuccess) {
        [oldHub warningUserAfterJump:message time:1.f];
        [self.delegate paySuccess];
        [self.superview removeFromSuperview];
        [self removeFromSuperview];
    }
    else
    {
        [oldHub warningUser:message];
    }
}

#pragma mark - btn
- (void)btn_weixin:(id)sender
{
    _type = WECHAT_PAY;
    [_btn_balance setImage:[UIImage imageNamed:@"支付选择空"] forState:UIControlStateNormal];
    [_btn_weixin setImage:[UIImage imageNamed:@"支付选择"] forState:UIControlStateNormal];
    [_btn_alipay setImage:[UIImage imageNamed:@"支付选择空"] forState:UIControlStateNormal];
}

- (void)btn_alipay_tapped:(id)sender
{
    _type = ALI_PAY;
    [_btn_balance setImage:[UIImage imageNamed:@"支付选择空"] forState:UIControlStateNormal];
    [_btn_weixin setImage:[UIImage imageNamed:@"支付选择空"] forState:UIControlStateNormal];
    [_btn_alipay setImage:[UIImage imageNamed:@"支付选择"] forState:UIControlStateNormal];
}

- (void)btn_balance_tapped:(id)sender
{
    _type = BALANCE_PAY;
    [_btn_balance setImage:[UIImage imageNamed:@"支付选择"] forState:UIControlStateNormal];
    [_btn_weixin setImage:[UIImage imageNamed:@"支付选择空"] forState:UIControlStateNormal];
    [_btn_alipay setImage:[UIImage imageNamed:@"支付选择空"] forState:UIControlStateNormal];
}

- (void)btn_confirm_tapped:(id)sender
{
    switch (_type) {
        case ALI_PAY:
            [self alipay];
            break;
        case WECHAT_PAY:
            [self wechatpay];
            break;
        default:
            [self consumepay];
            break;
    }
}

- (void)alipay
{
    [oldHub waitingWindow:@"支付中.."];
    
    [oldHub waitingWindow:@"支付中.."];
    
    NSString *type = PAY_TYPE;
    
    if (_recharge) {
        type = RECHARGE_TYPE;
    }
    
    if (!_price) {
        _price = _orderInfo.totalAmount;
    }
    
    [BALANCE_SINGLETON AliPay:_orderInfo.oid price:_price type:type success:^(id data) {
        [oldHub hideHUDWindow];
    } error:^(NSString *error) {
        [oldHub warningWindow:error];
    }];
}

- (void)wechatpay
{
    [oldHub waitingWindow:@"支付中.."];
    
    NSString *type = PAY_TYPE;
    
    if (_recharge) {
        type = RECHARGE_TYPE;
    }
    
    if (!_price) {
        _price = _orderInfo.totalAmount;
    }
    
    [BALANCE_SINGLETON WechatPay:_orderInfo.oid price:_price type:type success:^(id data) {
        [oldHub hideHUDWindow];
    } error:^(NSString *error) {
        [oldHub warningWindow:error];
    }];
}

- (void)consumepay
{
    [oldHub waitingWindow:@"支付中.."];
    
    [BALANCE_SINGLETON ConsumePay:_orderInfo.oid success:^(id data) {
        [oldHub hideHUDWindow];
        [oldHub warningUserAfterJump:@"支付成功" time:1.f];
        [self.delegate paySuccess];
        [self.superview removeFromSuperview];
        [self removeFromSuperview];
    } error:^(NSString *error) {
        [oldHub warningWindow:error];
    }];
}

#pragma mark - 动画
/**
 *  动画划出
 */
- (void)fadeIn
{
//    self.frame = CGRectMake([UIScreen mainScreen].bounds.origin.x, [UIScreen mainScreen].bounds.size.height- self.frame.size.height, self.frame.size.width, self.frame.size.height);
    self.alpha = 0.0;
    
    CGAffineTransform newTransform = CGAffineTransformMakeScale(0.2, 0.2);
    [self setTransform:newTransform];
    
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        CATransform3D transform = CATransform3DMakeScale(1, 1, 1.0);
        self.layer.transform = transform;
        self.alpha = 1.0;
    } completion:^(BOOL finished) {
        
    }];
}
/**
 *  动画关闭
 *
 */
-(void)tapChange:(UITapGestureRecognizer *)gureView
{
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:
     ^{
         CATransform3D transform = CATransform3DMakeScale(0.2, 0.2, 1.0);
         self.layer.transform = transform;
         self.alpha = 0.0;
     } completion:^(BOOL finished) {
         [self.superview removeFromSuperview];
         [self removeFromSuperview];
     }];
}

- (void)tapPay:(UITapGestureRecognizer *)gureView
{
    
}

@end
